package com.ch.clinking.adapter.Order;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ch.clinking.R;
import com.ch.clinking.Utils.NetUtils.DoCallBack;
import com.ch.clinking.Utils.NetUtils.OkHttpUtil;
import com.ch.clinking.ViewHolder.OrderListS2EViewHolder;
import com.ch.clinking.activity.EditOrderActivity;
import com.ch.clinking.bean.OrderS2EDto;

import java.util.HashMap;
import java.util.List;

public class OrderListViewS2EAdapter extends RecyclerView.Adapter<OrderListS2EViewHolder>{

    private List<OrderS2EDto> orderDtoList;
    private Activity context;
    private ProgressBar orderFragmentLoadingProgressBar;

    boolean color = true;

    public OrderListViewS2EAdapter(Activity activity, List<OrderS2EDto> orderDtoList, ProgressBar orderFragmentLoadingProgressBar) {
        this.orderDtoList = orderDtoList;
        this.context = activity;
        this.orderFragmentLoadingProgressBar = orderFragmentLoadingProgressBar;
    }


    @NonNull
    @Override
    public OrderListS2EViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_order_ste_layout, parent, false);
        return new OrderListS2EViewHolder(view);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull OrderListS2EViewHolder holder, int position) {

        OrderS2EDto orderDto = orderDtoList.get(position);

        Log.d("selectOrderByShopId", "orderDto:"+orderDto);

        // 设置数据
        holder.merTitle.setText(orderDto.getTitle());
        holder.state.setText(orderDto.getState());
        if (orderDto.getState().equals("待出货")) {
            // 设置背景
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.bg_order_state_no_text);
            holder.state.setBackground(drawable);

            holder.state.setTextColor(ContextCompat.getColor(context, R.color.order_state_no));
        }else if (orderDto.getState().equals("未下单")){
            // 设置背景
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.bg_order_state_no_make_text);
            holder.state.setBackground(drawable);

            holder.state.setTextColor(ContextCompat.getColor(context, R.color.order_state_no_make));
        } else if (orderDto.getState().equals("已出货")) {
            // 设置背景
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.bg_order_state_yes_text);
            holder.state.setBackground(drawable);

            holder.state.setTextColor(ContextCompat.getColor(context, R.color.order_state_yes));
        }
        holder.productNum.setText(orderDto.getProductNumber());

        // 清空表格内容
        holder.tableLayout.removeAllViews();

        // 动态生成表格内容
        getSize(orderDto, holder.tableLayout);

        // 设置默认占位图片
        Glide.with(context)
                .load(R.drawable.appicon)
                .into(holder.merImg);

        // 加载图片
        Glide.with(context)
                .load(OkHttpUtil.getInstance().getIMG_URL() + orderDto.getFirstImage())
                .placeholder(R.drawable.appicon)
                .diskCacheStrategy(DiskCacheStrategy.ALL)  // 缓存策略：缓存原始图片和转换后的图片
                .thumbnail(0.5f)  // 缩略图大小
                .error(R.drawable.appicon)
                .into(holder.merImg);

//        // 设置点击事件
//        holder.listOrderItemContainer.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent(context, EditOrderActivity.class);
//                intent.putExtra("orderDto", orderDto);
//                context.startActivity(intent);
//            }
//        });
//
    }


    @Override
    public int getItemCount() {
        return orderDtoList.size();
    }

    @SuppressLint("SetTextI18n")
    private void getSize(OrderS2EDto orderDto, TableLayout tableLayout) {
        color = true;
        TableRow sizeRow = new TableRow(context);
        TableRow stockRow = new TableRow(context);
        TableRow makeRow = new TableRow(context);
        sizeRow.setPadding(0, 0, 0, 0);
        stockRow.setPadding(0, 0, 0, 0);
        makeRow.setPadding(0, 0, 0, 0);
        TableRow.LayoutParams editTextParams = new TableRow.LayoutParams(120, TableRow.LayoutParams.MATCH_PARENT);
        editTextParams.gravity = Gravity.CENTER;


        TextView tableTitle = new TextView(context);
        tableTitle.setText("尺码");
        TextView plan = new TextView(context);
        plan.setText("计划");
        TextView tureMake = new TextView(context);
        tureMake.setText("实数");
        setTableLayout(tableTitle, plan, tureMake, sizeRow, stockRow, makeRow, editTextParams);


        if (orderDto.getActivate_XXS()==1) {
            TextView size = new TextView(context);
            size.setText("XXS");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getXXS()));
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_XXS()));
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }
        if (orderDto.getActivate_XS()==1) {
            TextView size = new TextView(context);
            size.setText("XS");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getXS()));
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_XXS()));
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }
        if (orderDto.getActivate_S()==1) {
            TextView size = new TextView(context);
            size.setText("S");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getS()));
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_S()));
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }
        if (orderDto.getActivate_M()==1) {
            TextView size = new TextView(context);
            size.setText("M");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getM())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_M())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_L()==1) {
            TextView size = new TextView(context);
            size.setText("L");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_L())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_XL()==1) {
            TextView size = new TextView(context);
            size.setText("XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getXL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_XXL()==1) {
            TextView size = new TextView(context);
            size.setText("XXL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getXXL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_XXL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_Plus_0XL()==1) {
            TextView size = new TextView(context);
            size.setText("0XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getPlus_0XL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_Plus_0XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_Plus_1XL()==1) {
            TextView size = new TextView(context);
            size.setText("1XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getPlus_1XL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_Plus_1XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_Plus_2XL()==1) {
            TextView size = new TextView(context);
            size.setText("2XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getPlus_2XL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_Plus_2XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_Plus_3XL()==1) {
            TextView size = new TextView(context);
            size.setText("3XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getPlus_3XL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_Plus_3XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_Plus_4XL()==1) {
            TextView size = new TextView(context);
            size.setText("4XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getPlus_4XL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_Plus_4XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_Plus_5XL()==1) {
            TextView size = new TextView(context);
            size.setText("5XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getPlus_5XL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_Plus_5XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }

        if (orderDto.getActivate_Plus_6XL()==1) {
            TextView size = new TextView(context);
            size.setText("6XL");
            TextView stock = new TextView(context);
            stock.setText(Integer.toString(orderDto.getPlus_6XL())); // 确保你有相应的 getter 方法
            TextView make = new TextView(context);
            make.setText(Integer.toString(orderDto.getMake_Plus_6XL())); // 确保你有相应的 getter 方法
            setTableLayout(size, stock, make, sizeRow, stockRow, makeRow, editTextParams);
        }


        tableLayout.addView(sizeRow);
        tableLayout.addView(stockRow);
        tableLayout.addView(makeRow);
    }

    private void setTableLayout(TextView size, TextView stock, TextView make, TableRow sizeRow, TableRow stockRow, TableRow makeRow, TableRow.LayoutParams editTextParams) {
        size.setLayoutParams(editTextParams);
        stock.setLayoutParams(editTextParams);
        make.setLayoutParams(editTextParams);
        size.setBackgroundColor(getCellColor());
        stock.setBackgroundColor(getCellColor());
        make.setBackgroundColor(getCellColor());
        if (color) {
            color = false;
        } else {
            color = true;
        }
        size.setWidth(120);
        stock.setWidth(120);
        make.setWidth(120);
        size.setPadding(0, 0, 0, 0);
        stock.setPadding(0, 0, 0, 0);
        make.setPadding(0, 0, 0, 0);
        size.setGravity(Gravity.CENTER);
        stock.setGravity(Gravity.CENTER);
        make.setGravity(Gravity.CENTER);
        sizeRow.addView(size);
        stockRow.addView(stock);
        makeRow.addView(make);
    }

    private int getCellColor() {
        if (color) {
            return Color.parseColor("#E7E7E7"); // 偶数索引
        } else {
            return Color.parseColor("#FFFFFF"); // 奇数索引
        }
    }

    private void showLoading() {
        orderFragmentLoadingProgressBar.setVisibility(View.VISIBLE);
        if (context != null) {
            context.getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                    WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        }
    }

    private void hideLoading() {
        orderFragmentLoadingProgressBar.setVisibility(View.GONE);
        if (context != null) {
            context.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        }
    }

}
